package com.ddwl.goods.dao;

import com.ddwl.common.constant.BaseEnum;
import com.ddwl.common.constant.Constant;
import com.ddwl.common.dao.TkDao;
import com.ddwl.goods.dao.mapper.LimitTimePurActiveMapper;
import com.ddwl.goods.dao.model.LimitTimePurActive;
import com.ddwl.schema.bo.goods.QueryLimitTimePurActiveBo;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import java.time.Instant;
import java.util.List;

/**
 * @author wenbin
 */
@Repository
public class LimitTimePurActiveDao extends TkDao<LimitTimePurActiveMapper, LimitTimePurActive> {

    public List<LimitTimePurActive> queryLimitTimePurActive(QueryLimitTimePurActiveBo bo) {
        Example example = getExample();
        example.and().andEqualTo("shopId",bo.getShopId())
        .andNotEqualTo("id",bo.getId());
        return baseMapper.selectByExample(example);
    }

    public List<LimitTimePurActive> queryOverPurActive() {
        Example example = getExample();
        example.and().andEqualTo("isEffect",1)
                .andLessThan("endTime",Instant.now());
                //.andCondition(" TIME_TO_SEC(TIMEDIFF(now(),end_time))<=60 ");
        return baseMapper.selectByExample(example);
    }

    public List<LimitTimePurActive> queryPageActive(QueryLimitTimePurActiveBo bo) {
        Example example = getExample();
        example.and().andEqualTo("shopId",bo.getShopId())
                .andEqualTo("id",bo.getId())
                .andEqualTo("isEffect",bo.getIsEffect())
                .andGreaterThanOrEqualTo("endTime",Instant.now());
        example.setOrderByClause("start_time asc limit 1 ");
        return baseMapper.selectByExample(example);
    }

    public List<LimitTimePurActive> queryActive(QueryLimitTimePurActiveBo bo) {
        Example example = getExample();
        example.and().andEqualTo("shopId",bo.getShopId())
                .andEqualTo("isEffect",bo.getIsEffect())
                .andEqualTo("id",bo.getId());
        example.setOrderByClause("start_time desc");
        return baseMapper.selectByExample(example);
    }

    public List<LimitTimePurActive> findProcessingActivity(String shopId) {
        Example example = getExample();
        example.and().andEqualTo("shopId",shopId)
                .andEqualTo("isEffect", BaseEnum.StatusEnum.ENABLE.getCode())
                .andGreaterThanOrEqualTo("endTime",Instant.now());
        return baseMapper.selectByExample(example);
    }
}
